int fetchLimit = params.fetchLimit as int
int timeZoneHour = session.userTimeZoneHour as int
int timeZoneMinute = session.userTimeZoneMinute as int

def sb = new StringBuilder('<tbody>')
def tentities = datastore.execute {
	from TranscationE
	sort desc by updatedTime
	where userId == session.user.id
	limit fetchLimit
}
def transcations = tentities
if(fetchLimit > tentities.size()) {
	fetchLimit = tentities.size()
}
use(groovy.time.TimeCategory) {
	
	for(int i = 0; i < fetchLimit; i++ ) {
		def transcation = transcations.get(i)
		def transcationTags = datastore.execute {
			from TranscationTagE
			sort asc by name
			where transcationId == transcation.key.id and userId == session.user.id
		}
		def transcationTag = new StringBuilder('')
		transcationTags.each { tag -> 
			transcationTag += "<a class='table-2-link' href='transcationViewForTag.groovy?transcationTagId=$tag.name'>#$tag.name</a> "
		}
		def s = i % 2 == 0 ? 'table-2-even-row' : 'table-2-odd-row'
		def amount = String.format("%.2f", transcation.amount)
		def time = transcation.createdTime + timeZoneHour.hours + timeZoneMinute.minutes
		def createdTime = time.format('dd-MM-yyyy HH:mm:ss')
		sb += """
	<tr class='$s'>
		<td class='table-2-cell' align="right">${i+1}</td>
		<td class='table-2-cell' align="right"><a class='table-2-link' href='transcationEdit.groovy?transcationKey=${transcation.key.id}'>$transcation.key.id</a></td>
		<td class='table-2-cell' align="right">$amount</td>
		<td class='table-2-cell' align="left">$transcation.description</td>
		<td class='table-2-cell' align="left">$transcationTag</td>
		<td class="table-2-cell" align="left">$createdTime</td>
	</tr>
	"""
	}
	sb += '''
</tbody>
'''
	
}
println sb.toString()
